package controller

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"
)

const uploadPath = "uploads"

func init() {
	if err := os.MkdirAll(uploadPath, os.ModePerm); err != nil {
		log.Fatalf("创建upload目录错误: %v", err)
	}
}

// 定义generateUniqueFilename函数
func generateUniqueFilename(originalFilename string) string {
	ext := filepath.Ext(originalFilename)
	name := strings.TrimSuffix(originalFilename, ext)
	filePath := filepath.Join(uploadPath, originalFilename)

	if _, err := os.Stat(filePath); err == nil {
		// 文件已存在，增加时间戳
		timestamp := time.Now().Format("20060102150405")
		return fmt.Sprintf("%s_%s%s", name, timestamp, ext)
	}

	return originalFilename
}
